package com.hopu.iocdemo1;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 电脑类
 */
@Component
public class Computer {

    //硬编码方式创建对象
//    private ICpu cpu = new IntelCpu();
//    private IMemory memory = new KingstonMemory();
    //使用反射+注解实现属性的注入
//    @ComputerComponent(type = IntelCpu.class)
    //通过自动装配，将属性对象注入进来
    @Qualifier("intel")
    @Autowired
    private ICpu cpu = null;
//    @ComputerComponent(type = KingstonMemory.class)
    @Resource
    private IMemory memory = null;
    @Value("小米2999")
    private String name;

    public ICpu getCpu() {
        return cpu;
    }

    public void setCpu(ICpu cpu) {
        this.cpu = cpu;
    }

    public IMemory getMemory() {
        return memory;
    }

    public void setMemory(IMemory memory) {
        this.memory = memory;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Computer(ICpu cpu, IMemory memory, String name) {
        this.cpu = cpu;
        this.memory = memory;
        this.name = name;
    }

    public Computer() {
    }

    public void start(){
        System.out.println(name+"电脑启动了");
        cpu.run();
        memory.read();
        memory.write();
    }
}
